@echo off
title Email Demo System Installation

echo.
echo ========================================
echo Email Demo System Installation
echo ========================================
echo.

REM Check Python installation
echo [1/7] Checking Python environment...
python --version >nul 2>&1
if errorlevel 1 (
    echo ERROR: Python not found
    echo.
    echo Please install Python 3.7 or higher:
    echo https://www.python.org/downloads/
    echo.
    pause
    exit /b 1
)

REM Display Python version
for /f "tokens=2" %%i in ('python --version 2^>^&1') do set PYTHON_VERSION=%%i
echo SUCCESS: Python version: %PYTHON_VERSION%

REM Check pip availability
echo.
echo [2/7] Checking pip...
pip --version >nul 2>&1
if errorlevel 1 (
    echo ERROR: pip not found
    pause
    exit /b 1
)

echo SUCCESS: pip available

REM Create virtual environment
echo.
echo [3/7] Creating virtual environment...
if exist "venv" (
    echo WARNING: Virtual environment already exists, skipping creation
) else (
    python -m venv venv
    if errorlevel 1 (
        echo ERROR: Failed to create virtual environment
        pause
        exit /b 1
    )
    echo SUCCESS: Virtual environment created successfully
)

REM Activate virtual environment
echo.
echo [4/7] Activating virtual environment...
call venv\Scripts\activate.bat

REM Upgrade pip
echo.
echo [5/7] Upgrading pip...
python -m pip install --upgrade pip

REM Install dependencies
echo.
echo [6/7] Installing project dependencies...
if not exist "requirements.txt" (
    echo ERROR: requirements.txt not found
    pause
    exit /b 1
)

pip install -r requirements.txt
if errorlevel 1 (
    echo ERROR: Failed to install dependencies
    pause
    exit /b 1
)

echo SUCCESS: Dependencies installed successfully

REM Create necessary directories
echo.
echo [7/7] Creating necessary directories...
if not exist "logs" mkdir logs
if not exist "instance" mkdir instance

REM Copy configuration file
echo.
echo Configuring environment file...
if not exist ".env" (
    if exist ".env.example" (
        copy ".env.example" ".env" >nul
        echo SUCCESS: Environment configuration file created
        echo.
        echo IMPORTANT NOTICE:
        echo Please edit .env file to configure your environment variables
    ) else (
        echo WARNING: .env.example file not found
    )
) else (
    echo SUCCESS: Environment configuration file already exists
)

REM Initialize database
echo.
echo Initializing database...
python -c "from app import create_app; app = create_app(); app.app_context().push(); print('Database initialized successfully')" 2>nul
if errorlevel 1 (
    echo WARNING: Database initialization may have failed, please check configuration
) else (
    echo SUCCESS: Database initialized successfully
)

REM Installation completed
echo.
echo ========================================
echo INSTALLATION COMPLETED!
echo ========================================
echo.
echo How to start:
echo 1. Double-click start.bat file
echo 2. Or run in command line: python run.py
echo.
echo Access URL: http://127.0.0.1:5000
echo.
echo Default admin account:
echo Username: admin
echo Password: admin123
echo.
echo For more information, please check README.md file
echo.

pause